/*
 * Copyright (c) 2019 Hitevision
 */

package tw.com.hitevision.whiteboard.android.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import java.util.ArrayList;
import java.util.List;

import tw.com.hitevision.whiteboard.android.R;

public class FileShareUtils {
    /**
     * Creates an intent to share an image file.
     *
     * @return an {@code intent} to pass to {@code startActivity()}, otherwise {@code null}
     * if sharing app is not installed.
     */
    public static Intent createShareIntent(Context context, String file) {
        final Intent intent = new Intent();
        intent.setClassName(context.getString(R.string.package_sharer), context.getString(R.string.activity_sharer));
        intent.putExtra("INSTALL",false);

        final List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (activities.size() > 0) {
            intent.putExtra(context.getString(R.string.sharer_file), file);
            return intent;
        } else {
            return null;
        }
    }

    /**
     * Creates an intent to share multiple image files.
     *
     * @return an {@code intent} to pass to {@code startActivity()}, otherwise {@code null}
     * if sharing app is not installed.
     */
    public static Intent createShareIntent(Context context, ArrayList<String> files) {
        final Intent intent = new Intent();
        intent.setClassName(context.getString(R.string.package_sharer), context.getString(R.string.activity_sharer));
        intent.putExtra("INSTALL",false);
        final List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (activities.size() > 0) {
            intent.putStringArrayListExtra(context.getString(R.string.sharer_multiple_files), files);
            return intent;
        } else {
            return null;
        }
    }
}
